﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ERP.ERPSystem.WarehouseManagement;
using ERP.ERPSystem.WarehouseManagement.Dto;
using ERP.Common.Results;

namespace ERP.Web.Controllers.WarehouseManagement
{
    /// <summary>
    /// 采购入库控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ProcurementwarehousingController : ControllerBase
    {
        private readonly IProcurementwarehousingServices _procurementwarehousingServices;

        public ProcurementwarehousingController(IProcurementwarehousingServices procurementwarehousingServices)
        {
            _procurementwarehousingServices = procurementwarehousingServices;
        }

        /// <summary>
        /// 分页查询采购入库列表
        /// </summary>
        /// <param name="queryDto">查询条件</param>
        /// <returns>分页结果</returns>
        [HttpPost("GetPagedList")]
        public async Task<IActionResult> GetPagedList([FromBody] ProcurementwarehousingQueryDto queryDto)
        {
            try
            {
                // 设置默认分页参数
                if (queryDto == null)
                {
                    queryDto = new ProcurementwarehousingQueryDto();
                }
                
                if (queryDto.Page <= 0) queryDto.Page = 1;
                if (queryDto.Limit <= 0) queryDto.Limit = 10;
                
                var result = await _procurementwarehousingServices.GetPagedListAsync(queryDto);
                return Ok(new { 
                    success = true,
                    message = "查询成功",
                    listDate = result.ListDate,
                    pageCount = result.PageCount, // 总记录数
                    pageIndex = result.PageIndex  // 总页数
                });
            }
            catch (Exception ex)
            {
                return BadRequest(new { 
                    success = false, 
                    message = "查询失败：" + ex.Message,
                    listDate = new List<ProcurementwarehousingDTO>(),
                    pageCount = 0,
                    pageIndex = 0
                });
            }
        }

        /// <summary>
        /// 根据ID获取采购入库详情
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>详情信息</returns>
        [HttpGet("GetById/{id}")]
        public async Task<ProcurementwarehousingDTO> GetById(int id)
        {
            try
            {
                return await _procurementwarehousingServices.GetByIdAsync(id);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 新增采购入库
        /// </summary>
        /// <param name="createDto">新增数据</param>
        /// <returns>操作结果</returns>
        [HttpPost("Create")]
        public async Task<IActionResult> Create([FromBody] ProcurementwarehousingCreateDto createDto)
        {
            try
            {
                var result = await _procurementwarehousingServices.CreateAsync(createDto);
                if (result)
                {
                    return Ok(new { success = true, message = "新增成功" });
                }
                else
                {
                    return BadRequest(new { success = false, message = "新增失败" });
                }
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = "系统异常：" + ex.Message });
            }
        }

        /// <summary>
        /// 生成入库单编号
        /// </summary>
        /// <returns>入库单编号</returns>
        [HttpGet("GenerateCode")]
        public IActionResult GenerateCode()
        {
            try
            {
                var code = _procurementwarehousingServices.GenerateInboundCode();
                return Ok(new { success = true, code = code });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = "生成编号失败：" + ex.Message });
            }
        }

        /// <summary>
        /// 删除采购入库记录
        /// </summary>
        /// <param name="ids">要删除的ID列表</param>
        /// <returns>操作结果</returns>
        [HttpPost("Delete")]
        public async Task<IActionResult> Delete([FromBody] List<int> ids)
        {
            try
            {
                if (ids == null || ids.Count == 0)
                {
                    return BadRequest(new { success = false, message = "请选择要删除的记录" });
                }

                // 这里暂时只做软删除处理，实际项目中应该根据业务需求决定
                foreach (var id in ids)
                {
                    var entity = await _procurementwarehousingServices.GetByIdAsync(id);
                    if (entity != null)
                    {
                        // 实际删除逻辑需要在Service中实现
                        // await _procurementwarehousingServices.DeleteAsync(id);
                    }
                }

                return Ok(new { success = true, message = "删除成功" });
            }
            catch (Exception ex)
            {
                return BadRequest(new { success = false, message = "删除失败：" + ex.Message });
            }
        }
    }
}
